﻿using System;
using System.Collections.Generic;
using System.Text;
using DirectX_Sample;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;

namespace DX1
{
    public class MyBox : MyObject
    {
        public MyBox(ref Device d) : base(ref d)           
        {

        }

        public void CreateMyBox(float X, float Y , float Z )
        {
            int NoV = 24;
            
            int NoF = 6 * 2 * 3;

            Vector3 vect = new Vector3(X, Y, Z);

            float d = vect.Length();  

            CustomVertex.PositionNormalTextured[] vertices = new CustomVertex.PositionNormalTextured[NoV];

            #region vertices
            vertices[0].Position = new Vector3(0,0,0);
            vertices[1].Position = new Vector3(0, Y, 0);
            vertices[2].Position = new Vector3(X, Y, 0);
            vertices[3].Position = new Vector3(X, 0, 0);
            vertices[4].Position = new Vector3(0, 0, Z);
            vertices[5].Position = new Vector3(0, Y, Z);
            vertices[6].Position = new Vector3(X, Y, Z);
            vertices[7].Position = new Vector3(X, 0, Z);

            vertices[8].Position = new Vector3(0, 0, 0);
            vertices[9].Position = new Vector3(0, Y, 0);
            vertices[10].Position = new Vector3(X, Y, 0);
            vertices[11].Position = new Vector3(X, 0, 0);
            vertices[12].Position = new Vector3(0, 0, Z);
            vertices[13].Position = new Vector3(0, Y, Z);
            vertices[14].Position = new Vector3(X, Y, Z);
            vertices[15].Position = new Vector3(X, 0, Z);

            vertices[16].Position = new Vector3(0, 0, 0);
            vertices[17].Position = new Vector3(0, Y, 0);
            vertices[18].Position = new Vector3(X, Y, 0);
            vertices[19].Position = new Vector3(X, 0, 0);
            vertices[20].Position = new Vector3(0, 0, Z);
            vertices[21].Position = new Vector3(0, Y, Z);
            vertices[22].Position = new Vector3(X, Y, Z);
            vertices[23].Position = new Vector3(X, 0, Z);
            #endregion

            int[] indices = new int[NoF];

            #region indices
            
            //1.1.1
            indices[0] = 1;
            indices[1] = 2;
            indices[2] = 0;

            //1.1.2
            indices[3] = 2;
            indices[4] = 3;
            indices[5] = 0;

            //3.1.1
            indices[6] = 16;
            indices[7] = 19;
            indices[8] = 23;

            //3.1.2
            indices[9] = 16;
            indices[10] = 23;
            indices[11] = 20;

            //1.2.1
            indices[12] = 4;
            indices[13] = 7;
            indices[14] = 5;

            //1.2.2
            indices[15] = 5;
            indices[16] = 7;
            indices[17] = 6;

            //3.2.1
            indices[18] = 21;
            indices[19] = 22;
            indices[20] = 18;

            //3.2.2
            indices[21] = 21;
            indices[22] = 18;
            indices[23] = 17;

            //2.1.1
            indices[24] = 15;
            indices[25] = 10;
            indices[26] = 14;

            //2.1.2
            indices[27] = 15;
            indices[28] = 11;
            indices[29] = 10;

            //2.2.1
            indices[30] = 8;
            indices[31] = 13;
            indices[32] = 9;

            //2.2.2
            indices[33] = 8;
            indices[34] = 12;
            indices[35] = 13;
            #endregion 

            #region
            vertices[0].Tu = 0;
            vertices[0].Tv = 0;
            vertices[3].Tu = 1;
            vertices[3].Tv = 0;
            vertices[2].Tu = 1;
            vertices[2].Tv = 1;
            vertices[1].Tu = 0;
            vertices[1].Tv = 1;

            vertices[4].Tu = 0;
            vertices[4].Tv = 0;
            vertices[7].Tu = 1;
            vertices[7].Tv = 0;
            vertices[6].Tu = 1;
            vertices[6].Tv = 1;
            vertices[5].Tu = 0;
            vertices[5].Tv = 1;

            //--
            vertices[16].Tu = 0;
            vertices[16].Tv = 0;
            vertices[19].Tu = 1;
            vertices[19].Tv = 0;
            vertices[23].Tu = 1;
            vertices[23].Tv = 1;
            vertices[20].Tu = 0;
            vertices[20].Tv = 1;

            vertices[17].Tu = 0;
            vertices[17].Tv = 0;
            vertices[18].Tu = 1;
            vertices[18].Tv = 0;
            vertices[22].Tu = 1;
            vertices[22].Tv = 1;
            vertices[21].Tu = 0;
            vertices[21].Tv = 1;

            //--
            vertices[15].Tu = 0;
            vertices[15].Tv = 0;
            vertices[11].Tu = 1;
            vertices[11].Tv = 0;
            vertices[10].Tu = 1;
            vertices[10].Tv = 1;
            vertices[14].Tu = 0;
            vertices[14].Tv = 1;

            vertices[8].Tu = 0;
            vertices[8].Tv = 0;
            vertices[9].Tu = 1;
            vertices[9].Tv = 0;
            vertices[13].Tu = 1;
            vertices[13].Tv = 1;
            vertices[12].Tu = 0;
            vertices[12].Tv = 1;
            #endregion

            #region normals

            vertices[0].Normal = new Vector3(0, 0, -X * Y);
            vertices[1].Normal = new Vector3(0, 0, -X * Y);
            vertices[2].Normal = new Vector3(0, 0, -X * Y);
            vertices[3].Normal = new Vector3(0, 0, -X * Y);

            vertices[4].Normal = new Vector3(0, 0, X * Y);
            vertices[5].Normal = new Vector3(0, 0, X * Y);
            vertices[6].Normal = new Vector3(0, 0, X * Y);
            vertices[7].Normal = new Vector3(0, 0, X * Y);

            vertices[8].Normal = new Vector3(-Y * Z, 0, 0);
            vertices[9].Normal = new Vector3(-Y * Z, 0, 0);
            vertices[10].Normal = new Vector3(-Y * Z, 0, 0);
            vertices[11].Normal = new Vector3(-Y * Z, 0, 0);

            vertices[12].Normal = new Vector3(Y * Z, 0, 0);
            vertices[13].Normal = new Vector3(Y * Z, 0, 0);
            vertices[14].Normal = new Vector3(Y * Z, 0, 0);
            vertices[15].Normal = new Vector3(Y * Z, 0, 0);

            vertices[16].Normal = new Vector3(0, -X * Z, 0);
            vertices[17].Normal = new Vector3(0, -X * Z, 0);
            vertices[18].Normal = new Vector3(0, -X * Z, 0);
            vertices[19].Normal = new Vector3(0, -X * Z, 0);

            vertices[20].Normal = new Vector3(0, X * Z, 0);
            vertices[21].Normal = new Vector3(0, X * Z, 0);
            vertices[22].Normal = new Vector3(0, X * Z, 0);
            vertices[23].Normal = new Vector3(0, X * Z, 0);
            #endregion



            m = new Mesh(NoF, NoV, MeshFlags.Managed | MeshFlags.Use32Bit, CustomVertex.PositionNormalTextured.Format, dev);

            m.IndexBuffer.SetData(indices, 0, LockFlags.None);
            m.VertexBuffer.SetData(vertices, 0, LockFlags.None);

            //m.ComputeNormals();
            CalculateBounds();
            ExtendedMaterials = new ExtendedMaterial[1];
            Textures = new Texture[1];
            Materials = new Material[1];
            ExtendedMaterials[0] = new ExtendedMaterial();
            Textures[0] = null;
            Materials[0] = new Material();
            Materials[0].Ambient = Color.White;
            Materials[0].Diffuse = Color.White;
        }

        public int StepX { get; set; }
    }
}
